/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Dao;

import BackEnd.Exceptions.*;
import com.mysql.jdbc.*;
import com.mysql.jdbc.ResultSet;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Types;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ivan
 */
public class ComentariopostDao extends DaoBase
{

    @Override
    public ValueObjectBase create(ValueObjectBase value) throws CreateRegisterException{
        if(value.getClass()==ComentariopostValueObject.class)
        {
            ComentariopostValueObject vo=(ComentariopostValueObject)value;
            try {
            Connection con=super.getConection("dlc");
            String sentencia="call pa_registrarNuevoComentario(?,?,?,?,?,?)";
            java.sql.CallableStatement wcall=con.prepareCall(sentencia);
            wcall.setInt(1, vo.getId());
            wcall.setInt(2,vo.getIdUsuario());
            wcall.setString(3, vo.getContenido());
            wcall.setString(4, "@var1");
            wcall.setString(5, "@var2");
            wcall.setString(6, "@var3");
            wcall.registerOutParameter(4, Types.DATE);
            wcall.registerOutParameter(5, Types.TIME);
            wcall.registerOutParameter(6, Types.INTEGER);
            wcall.execute();
            vo.setFecha(wcall.getDate(4));
            vo.setHora(wcall.getTime(5));
            vo.setId(wcall.getInt(6));
            wcall.close();
            con.close();
        } catch (SQLException ex) {
            throw new CreateRegisterException();

        }
             return vo;
        }
        else
            throw new CreateRegisterException();
    }

    @Override
    public void update(ValueObjectBase value) throws UpdateRegisterException{
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ValueObjectBase retrive(PkBase pk) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public ValueObjectBase[] traerComentarios(int idPost)
    {
        return this.mostrarConsulta("select * from comentariopost where idPost="+idPost);
    }

    @Override
    public void delete(PkBase pk) throws DeleteRegisterException{
        if(pk.getClass()==PkId.class)
        {
            PkId id=(PkId )pk;
            
            try {
                Connection con = super.getConection("dlc");
                String sentencia="call pa_eliminarComentario(?)";
                java.sql.CallableStatement wcall=con.prepareCall(sentencia);
                wcall.setInt(3, id.getId());
                wcall.execute();
                wcall.close();
                con.close();
             } catch (SQLException ex) {
                throw new DeleteRegisterException();
            }
        }
    }

    @Override
    public ValueObjectBase[] listarTodos() {
        return this.mostrarConsulta("select * from comentariopost");
    }

    @Override
    public ValueObjectBase[] mostrarConsulta(String consulta) {
        ComentariopostValueObject[] vec=null;
            try {
                Connection con = super.getConection("dlc");
                Statement st=(Statement)con.createStatement();
                st.execute(consulta);
                ResultSet res=(ResultSet) st.getResultSet();
                res.last();
                vec = new ComentariopostValueObject[res.getRow()];
                res.first();
                vec[0]=this.leerVo(res);
                while(res.next())
                {
                    vec[res.getRow()-1]=this.leerVo(res);
                }
                res.close();
                con.close();

            } catch (SQLException ex) {
                return null;
            }
        return vec;
    }

    @Override
    public ValueObjectBase traer(PkBase pk) throws NotExistRegisterException {

        if(pk.getClass() == PkId.class)
        {
             PkId id=(PkId)pk;
            ComentariopostValueObject vo = new ComentariopostValueObject();
            try {
                Connection con = super.getConection("dlc");
                String sentencia = "select * from comentariopost where id="+id.getId();
                Statement st=(Statement)con.createStatement();
                st.execute(sentencia);
                ResultSet res=(ResultSet) st.getResultSet();
                res.next();
                vo=this.leerVo(res);
                res.close();
                con.close();

            } catch (SQLException ex) {
                throw new NotExistRegisterException();
            }

            return vo;
        }
        else
            throw new NotExistRegisterException();
    }
    private ComentariopostValueObject leerVo(ResultSet res) throws SQLException
    {
        ComentariopostValueObject vo=new ComentariopostValueObject();
        vo.setId(res.getInt("id"));
        vo.setIdPost(res.getInt("idPost"));
        vo.setFecha(res.getDate("fecha"));
        vo.setHora(res.getTime("hora"));
        vo.setTitulo(res.getString("titulo"));
        vo.setContenido(res.getString("contenido"));
        vo.setIdUsuario(res.getInt("idUsuario"));
        vo.setIdModerador(res.getInt("idModerador"));
        vo.setMotivoModeracio(res.getString("motivoModeracion"));
        vo.setFechaModeracion(res.getDate("fechaModeracion"));

        return vo;
    }

}
